<?php
namespace app\commission\controller;
use app\AdminRest;
use app\ApiRest;
use longbingcore\wxcore\WxPay;
use think\App;
use app\commission\model\Cash as Model;
use app\commission\model\Water;
use app\commission\model\Wallet;

use think\facade\Cache;
use think\facade\Db;

class AdminWallet extends AdminRest
{

    protected $model;

    protected $water_model;

    protected $wallet_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model        = new Model();

        $this->water_model  = new Water();

        $this->wallet_model = new Wallet();
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:30
     * @return void
     * descrption:提现记录列表
     */
    public function walletList(){

       $input = $this->_input;

       $dis[] = ['a.uniacid','=',$this->_uniacid];

       if(!empty($input['status'])){

           $dis[] = ['a.status','=',$input['status']];
       }
       $where = [];

       if(!empty($input['name'])){

           $where[] = ['b.nickName','like','%'.$input['name'].'%'];

           $where[] = ['c.name','like','%'.$input['name'].'%'];

       }

       $data = $this->wallet_model->adminWalletList($dis,$where,$input['limit']);

       return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-23 17:48
     * @功能说明:驳回提现
     */
    public function noPass(){

        $input = $this->_input;

        $info = $this->wallet_model->walletInfo(['id'=>$input['id']]);

        if(empty($info)){

            $this->errorMsg('记录不存在');
        }

        if($info['status']!=1){

            $this->errorMsg('记录状态错误');

        }

        Db::startTrans();
        //修改提现状态
        $res = $this->wallet_model->walletUpdate(['id'=>$input['id'],'status'=>1],['status'=>3]);

        if($res != 1){

            $this->errorMsg('驳回失败');

        }
        //返回佣金
        $res = $this->model->incOrDecCash(['user_id'=>$info['user_id'],'uniacid'=>$info['uniacid']],$info['true_price']);

        Db::commit();

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-23 17:59
     * @功能说明:通过提现
     */
    public function pass(){

        $input = $this->_input;

        $info = $this->wallet_model->walletInfo(['id'=>$input['id']]);

        if(empty($info)){

            $this->errorMsg('记录不存在');
        }

        if($info['status']!=1){

            $this->errorMsg('记录状态错误');

        }

        Db::startTrans();
        //修改提现状态
        $res = $this->wallet_model->walletUpdate(['id'=>$input['id'],'status'=>1],['status'=>2]);

        if($res != 1){

            $this->errorMsg('提现失败');
        }
        //获取用户openid
        $openid = Db::name('longbing_card_user')->where(['id'=>$info['user_id']])->value('openid');
        //打款金额
        $money  = round($info['true_price'],2);
        //微信相关模型
        $wx_pay = new WxPay($this->_uniacid);
        //微信提现
        $res    = $wx_pay->crteateMchPay($openid,$money);

        if($res['result_code']=='SUCCESS'&&$res['return_code']=='SUCCESS'){
            //if(1==1){

        }else{

            return $this->error(!empty($res['err_code_des'])?$res['err_code_des']:'你还未该权限');

        }

        Db::commit();

        return $this->success($res);
    }




}
